{include file="public/header,public/nav,public/fixbar" title="<?php echo $info['name']?>-<?php echo request()->pc_info['seo_title']?>"/}
<style>
    .layui-card-body p img {
        width: 100%;
    }
</style>
<div class="layui-body">
    <div class="layui-container">
        <div class="layui-main margin-bottom-10">
            <div class="layui-col-space30 item">
                <div class="layui-col-xs12 layui-col-md6 layui-col-lg4">
                    <div class="product-gallery">
                        <img src="{$info['head_imgs'][0]}" alt="商品主图" class="main-image" data-image>
                        <div class="thumbnails">
                            {if count($info['head_imgs']) > 1}
                                {foreach $info['head_imgs'] as $key => $v}
                                <img src="{$v}" alt="缩略图{$key}" class="thumbnail {if $key == 0} active {/if}">
                                {/foreach}
                            {/if}
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-md6 layui-col-lg8">
                    <div class="product-info">
                        <h1>{$info.name}</h1>
                        <div class="product-desc layui-text">
                            <p>
                                {if $info.sub_title}
                                <span class="margin-right-10">{$info.sub_title}</span>
                                {/if}
                                {foreach $info.tags as $v}
                                    {if $v}
                                        <span class="layui-btn layui-btn-primary layui-btn-xs">{$v}</span>
                                    {/if}
                                {/foreach}
                                
                                <span class="layui-btn layui-btn-primary layui-btn-xs">已售 {$info.real_sales+$info.virtual_sales}</span>
                                <span class="layui-btn layui-btn-primary layui-btn-xs">{if $info.is_free_shipping == 0} 不包邮 {else} 包邮 {/if}</span>
                            </p>
                        </div>
                        <div class="price">
                            <span class="current-price">¥{$info.sale_price}</span>
                            <span class="original-price">¥{$info.original_price}</span>
                        </div>
                        <!-- 说明 -->
                        {if $info.is_member_discount == 0 || $info.gift_integral > 0}
                        <div>
                            <span>说明：{if $info.is_member_discount == 0} 该商品不参与会员折扣， {/if}购买可得 <b>{$info.gift_integral}</b> 积分</span>
                        </div>
                        {/if}
                        <!-- 规格选择 -->
                        <div class="product-spec">
                            <div class="spec-item">
                                <div class="title">可选规格</div>
                                <div class="spec-options">
                                    {foreach $info.attr as $key => $v}
                                    <div class="spec-option {if $key == 0}active{/if}" data-id="{$v.id}">{$v.name}</div>
                                    {/foreach}
                                </div>
                            </div>
                        </div>
                        <!-- 数量选择 -->
                        <div class="quantity-selector">
                            <span>数量：</span>
                            <div class="quantity-btn" id="decrease">
                                <i class="layui-icon layui-icon-subtraction"></i>
                            </div>
                            <input type="text" name="goods_num" class="quantity-input positive-integer" value="1" id="quantity">
                            <div class="quantity-btn" id="increase">
                                <i class="layui-icon layui-icon-addition"></i>
                            </div>
                            <span style="margin-left: 10px;">库存：<span class="stock-num">{$info.attr[0]['stock_num']}</span>件</span>
                        </div>
                        
                        <!-- 操作按钮 -->
                        <div class="action-buttons">
                            {if $info.status == 0}
                            <button class="layui-btn layui-btn-disabled layui-btn-lg">
                                已下架
                            </button>
                            {else}
                            <button class="layui-btn layui-bg-red layui-btn-lg order-prepare" data-id="{$info.id}">
                                立即购买
                            </button>
                            {/if}
                            <button class="layui-btn layui-btn-primary layui-btn-lg favorites" user-login data-id="{$info.id}">
                                <i style="font-size: 20px;" class="layui-icon {if $info.is_favorites} layui-icon-star-fill active {else} layui-icon-star {/if}"></i> 收藏
                            </button>
                        </div>
                        
                        <!-- 服务承诺 -->
                        <div style="margin-top: 20px;display: flex">
                            <span style="display: flex;align-items: center;">
                                <i class="iconfont" style="margin-right: 5px;font-size: 18px;">&#xe66e;</i> 正品保证
                            </span>
                            {if $info.after_sales_service && $info.after_sales_service.seven_day_no_reason_return == 1}
                            <span style="margin-left: 15px;display: flex;align-items: center;">
                                <i class="iconfont" style="margin-right: 5px;font-size: 18px;">&#xe608;</i> 七天无理由退货
                            </span>
                            {/if}
                            <span style="margin-left: 15px;display: flex;align-items: center;">
                                <i class="iconfont" style="margin-right: 5px;font-size: 18px;">&#xe606;</i> 极速发货
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 商品详情标签页 -->
        <div class="layui-col-space10">
            <div class="layui-col-xs12 layui-col-md8 layui-col-lg8">
                <div class="layui-card">
                    <div class="layui-card-body">
                        {$info.content|raw}
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12 layui-col-md4 layui-col-lg4">
                <div class="layui-main">
                    <div class="layui-row">
                        <div class="layui-font-16 margin-bottom-15 font-weight-bold">
                            <i class="layui-icon layui-icon-fire layui-font-16 padding-right-5 layui-font-red"></i>
                            相关商品
                        </div>
                        <div class="layui-col-space15">
                            {foreach $related_list as $v}
                            <div class="layui-col-xs12 article-li">
                                <a href="{$v.href}">
                                    <div class="layui-card">
                                        <div class="layui-card-body">
                                            <div class="item">
                                                <img src="{$v.head_imgs}" class="user-goods-img">
                                                <div class="detail">
                                                    <div class="product-title layui-ellips">
                                                        <span class="item-title-label">{$v.product_type_text}</span>{$v.name}
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="layui-card-footer">
                                            <p class="text">已售 <b>{$v.virtual_sales+$v.real_sales}</b></p>
                                            {if $v.sale_price == 0}
                                            <p class="product-price layui-font-16">免费</p>
                                            {else}
                                            <p class="product-price layui-font-16"><span class="symbol">￥</span>{$v.sale_price}</p>
                                            {/if}
                                        </div>
                                    </div>
                                </a>
                            </div>
                            {/foreach}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
    </div>
</div>
{include file="public/footer"/}
<script type="text/javascript">
    layui.use(['element', 'layer'], function(){
            var element = layui.element;
            var layer = layui.layer;
            var $ = layui.$;
        
        // 缩略图点击事件
        $('.thumbnail').click(function(){
            $('.thumbnail').removeClass('active');
            $(this).addClass('active');
            var src = $(this).attr('src');
            $('.main-image').attr('src', src);
        });

        // 规格选择
        $('.spec-option').click(function(){
            var loading = layer.load();
            var id = $(this).data('id');
            $(this).parent().find('.spec-option').removeClass('active');
            $(this).addClass('active');
            $.ajax({
                url:"/goods/get_attr_info",
                type:'post',
                data:{"attr_id":id},
                dataType:'JSON',
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                success:function(res){
                    layer.close(loading);
                    if(res.code==0){
                        $(".stock-num").text(res.data.stock_num);
                        $(".current-price").text('￥'+res.data.sale_price);
                        $(".original-price").text('￥'+res.data.original_price);
                    }else{
                        layer.msg(res.msg,{icon: 2,time: 2000});
                    }
                },
                error:function(e){
                    layer.msg('请求失败'+e.responseText,{icon: 2});
                },
            });
            
        });
        
        // 数量增减
        $('#increase').click(function(){
            var quantity = parseInt($('#quantity').val());
            $('#quantity').val(quantity + 1);
        });
        
        $('#decrease').click(function(){
            var quantity = parseInt($('#quantity').val());
            if(quantity > 1) {
                $('#quantity').val(quantity - 1);
            }
        });

        // 商品收藏/取消
        $(".favorites").click(function(){
            var loading = layer.load();
            var product_id = $(this).data('id');
            $.ajax({
                url:"/user/favorites_product",
                type:'post',
                data:{"product_id":product_id},
                dataType:'JSON',
                headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                success:function(res){
                    layer.close(loading);
                    if(res.code==0){
                        layer.msg(res.msg,{icon: 1,time: 1500},function(){
                            if (res.data == 1) {
                                // 收藏
                                $(".favorites").find('i').removeClass('layui-icon-star-fill active layui-icon-star');
                                $(".favorites").find('i').addClass('layui-icon-star-fill active');
                            } else {
                                // 取消收藏
                                $(".favorites").find('i').removeClass('layui-icon-star-fill active layui-icon-star');
                                $(".favorites").find('i').addClass('layui-icon-star');
                            }
                        });
                    }else{
                        layer.msg(res.msg,{icon: 2,time: 2000});
                    }
                },
                error:function(e){
                    layer.msg('请求失败'+e.responseText,{icon: 2});
                },
            });
        });

        // 去购买
        $(".order-prepare").click(function() {
            var user_id = "{:session('user.id')}";
            if (!user_id) {
                open_login();
                return false;
            }
            var attr_id = $('.spec-option.active').data('id');
            var id = $(this).data('id');
            var goods_num = $("input[name=goods_num]").val();
            location.href = '/order/prepare?id='+id+'&attr_id='+attr_id+'&num='+goods_num
        })
    });
</script>